package com.shd.edanpei.presenter;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.ProductBean;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.presenter.contract.ProductContract;
import com.shd.edanpei.util.LayoutUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/3/28.
 * yang-wen-wen
 */
public class ProductPresenter extends BasePresenterImpl<ProductContract.ProductIView>
        implements ProductContract.ProductIPresenter {

    private int currentPage;
    private int totalPage;

    public ProductPresenter(ProductContract.ProductIView mView) {
        super(mView);
    }

    @Override
    public void refresh(int type) {
        currentPage = 1;
        ApiCreator.getApi().productList(type, currentPage)
                .compose(RxTransformer.<PagingModel<ProductBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<ProductBean>>() {
                    @Override
                    protected void onSuccess(PagingModel<ProductBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.getAdapter()
                                    .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_message, "暂无提供，请创建"));
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter()
                                .setEmptyView(LayoutUtils.getNullLayout(mBaseIView.getContext(), R.drawable.null_message, "暂无提供，请创建"));
                    }

                    @Override
                    public void onFinish() {
                        if (mBaseIView != null) {
                            mBaseIView.getRefreshLayout().setRefreshing(false);
                        }
                    }
                });
    }

    @Override
    public void loadMore(int type) {
        ApiCreator.getApi().productList(type, currentPage)
                .compose(RxTransformer.<PagingModel<ProductBean>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<ProductBean>>() {
                    protected void onSuccess(PagingModel<ProductBean> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;

                        } else {
                            mBaseIView.getAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter().loadMoreEnd();
                    }
                });
    }

    @Override
    public void checkList(int type) {
        if (mBaseIView.getAdapter().getData().size() == 0) {
            mBaseIView.getAdapter().setEmptyView(getNullLayout(type));
        }
    }

    @Override
    public void delete(String id, final int position) {
        ApiCreator.getApi().deleteProduct(id)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        mBaseIView.getAdapter().remove(position);
                    }
                });
    }

    private View getNullLayout(final int type) {
        View view = LayoutInflater.from(mBaseIView.getContext()).inflate(R.layout.null_layout, null);
        ImageView ivPic = view.findViewById(R.id.iv_null_icon);
        TextView tvHint = view.findViewById(R.id.tv_null_hint);
        ivPic.setImageResource(R.drawable.pic_null_bg1);
        if (type == 1) {
            tvHint.setText("您还未添加相关产品，请尽快添加吧");
        } else {
            tvHint.setText("您还没有相关产品");
        }
        return view;
    }
}
